import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:speech_play/speech_play.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final speechPlay = SpeechPlay();

  @override
  void initState() {
    super.initState();
    initPlatformState();
    initPlay();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await speechPlay.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<void> initPlay() async{
    speechPlay.addEventListener(
      onListenerCompleted: (Map<String, dynamic> data) async{
        print(data);
      },
      onListenerPlayTime: (Map<String, dynamic> data) async {
        print(data);
      },
      onSwitchSpeaker: (Map<String, dynamic> data) async {
        print(data);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: [
            Text('Running on: $_platformVersion\n'),
            TextButton(
                onPressed: () {
                  speechPlay.speechPlay("https://hr.facesteel.com:9088/upload/downloadSource?path=/upload/20250610/08/0b7e22aa54364e619740676ec3d6e1dct2B0PG.mp4-enc&fileName=0b7e22aa54364e619740676ec3d6e1dct2B0PG.mp4",
                      0,
                      0)
                      .then((data){
                    print(data);
                  });
                },
                child: Text('播放语音')
            ),
            TextButton(
                onPressed: () {
                  speechPlay.pausePlay().then((time) {
                    print(time);
                  });
                },
                child: Text('暂停播放')
            ),
            TextButton(
                onPressed: () {
                  speechPlay.cancelPause();
                },
                child: Text('取消暂停')
            ),
            TextButton(
                onPressed: () {
                  speechPlay.playSeekTo(10 * 1000);
                },
                child: Text('跳转到第10秒')
            ),
            TextButton(
                onPressed: () {
                  speechPlay.switchSpeaker(true);
                },
                child: Text('切换扬声器')
            ),
            TextButton(
                onPressed: () {
                  speechPlay.switchSpeaker(false);
                },
                child: Text('切换听筒')
            ),

          ],
        ),
      ),
    );
  }
}
